package com.noble.glamour.module.logic.model.impl;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.meituan.android.walle.WalleChannelReader;
import com.noble.meether.BuildConfig;
import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.config.Constant;
import com.noble.glamour.module.logic.config.ServerTime;
import com.noble.glamour.module.logic.model.mgr.AppMgr;
import com.noble.library.log.PSP;
import com.noble.library.utils.ViewUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * 应用状态与手机硬件信息获取
 * Created by ZRP on 2016/12/29.
 */
public class AppMgrImpl implements AppMgr {

    private static final String DEVICE_SCREEN_HEIGHT = "DEVICE_SCREEN_HEIGHT";

    private String packageName = "";    // 软件包名
    private String signature = "";      // 软件签名
    private int versionCode = 0;        // 软件版本号
    private String versionName = "";    // 软件版本名称

    @SuppressLint("HardwareIds")
    @Override
    public void init() {
        try {
            PackageManager packageManager = App.context.getPackageManager();
            PackageInfo packInfo;
            packInfo = packageManager.getPackageInfo(App.context.getPackageName(), 0);
            // 获取软件基本信息
            versionCode = packInfo.versionCode;
            versionName = packInfo.versionName;
            packageName = packInfo.packageName;

            // 获取签名
            List<PackageInfo> apps = packageManager.getInstalledPackages(PackageManager.GET_SIGNATURES);
            for (PackageInfo packageinfo : apps) {
                String thisName = packageinfo.packageName;
                if (thisName.equals(getPackageName())) {
                    signature = packageinfo.signatures[0].toCharsString();
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void release() {
    }

    // ================渠道与包信息================
    @Override
    public String getChannel() {
        String channel = WalleChannelReader.getChannel(App.getContext());
        PSP.getInstance().put(Constant.APP_CHANNEL, channel);
        return TextUtils.isEmpty(channel) ? "0" : channel;
    }

    @Override
    public String getPackageName() {
        return packageName;
    }

    @Override
    public String getSignature() {
        return signature;
    }

    @Override
    public int getVerCode() {
        return versionCode;
    }

    @Override
    public String getVerName() {
        return versionName;
    }

    // ================软件状态信息================
    @Override
    public String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return "";
    }

    @Override
    public boolean isDebug() {
        return BuildConfig.DEBUG;
    }

    @Override
    public boolean isForeground() {
        ActivityManager activityManager = (ActivityManager) App.context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(App.context.getPackageName())) {
                return appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
            }
        }
        return true;
    }

    @Override
    public long getTime() {
        return ServerTime.getServerTime().getTimeInMillis();
    }

    @Override
    public long getSecondTime() {
        return ServerTime.getServerTime().getTimeInMillis() / 1000;
    }

    @Override
    public int getScreenHeight() {
        int screenHeight = PSP.getInstance().getInt(DEVICE_SCREEN_HEIGHT, -1);
        if (screenHeight <= 0) {
            try {
                screenHeight = ViewUtils.getScreenHeight((Activity) App.getActivity());
                PSP.getInstance().put(DEVICE_SCREEN_HEIGHT, screenHeight);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return screenHeight;
    }

    @Override
    public int getStatusBarHeight() {
        return ViewUtils.getStatusBarHeight(App.context);
    }
}
